﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using EquipmentConfig.Class;
using System.IO;

namespace EquipmentConfig
{
    public partial class CameraPicture : Form
    {
        private MainForm mf;
        private int dType;
        private int dIndex;

        public CameraPicture(MainForm mainForm, int debugType, int index)
        {
            InitializeComponent();
            mf = mainForm;
            dType = debugType;
            dIndex = index;
            btnUpLoadData.Enabled = false;
        }

        public void ShowInfo(byte type)
        {
            this.Invoke((MethodInvoker)delegate
            {
                btnStartCollect.Enabled = false;
                if (type == 0x01)
                {
                    tsslInfo.Text = "开始采集图片，请等待......";
                }

                if (type == 0x02)
                {
                    tsslInfo.Text = "采集图片中";
                }

                if (type == 0x03)
                {
                    mf.isCollectImage = true;
                    //tsslInfo.Text = "采集图片开始上传";
                }

                if (type == 0xF1)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "采集图片失败";
                    if (!btnStartCollect.Enabled)
                    {
                        btnStartCollect.Enabled = true;   
                    }

                    if (!btnUpLoadData.Enabled)
                    {
                        btnUpLoadData.Enabled = true;
                    }
                    mf.imageDataPackages.Clear();
                    mf.timer2.Enabled = true;
                }

                if (type == 0x04)
                {
                    tsslInfo.Text = "图片上传至服务器中";
                }

                if (type == 0x05)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "图片上传至服务器成功";
                    btnUpLoadData.Enabled = true;
                    mf.timer2.Enabled = true;
                }

                if (type == 0xF2)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "图片上传至服务器失败";
                    btnUpLoadData.Enabled = true;
                    mf.timer2.Enabled = true;
                }

                if (type == 0x06)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "采集图片完成";
                    btnStartCollect.Enabled = true;
                    btnUpLoadData.Enabled = true;
                    mf.imageDataPackages.Clear();
                    mf.timer2.Enabled = true;
                }

                if (type == 0x07)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "采集图片超时";
                    btnStartCollect.Enabled = true;
                    mf.imageDataPackages.Clear();
                    mf.timer2.Enabled = true;
                }

                if (type == 0xFF)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "采集图片缓存不存在";
                    btnStartCollect.Enabled = true;
                    mf.imageDataPackages.Clear();
                    mf.timer2.Enabled = true;
                }

                if (type == 0x08)
                {
                    mf.RealDataType = 0;
                    tsslInfo.Text = "上报图片超时";
                    btnUpLoadData.Enabled = true;
                    mf.timer2.Enabled = true;
                }
            });
        }

        public void ShowInfo(string strMsg)
        {
            this.Invoke((MethodInvoker)delegate
            {
                 tsslInfo.Text = strMsg;
            });
        }

        public void ShowImage(string strPath)
        {
            this.BeginInvoke((MethodInvoker)delegate
            {
                try
                {
                    FileStream files = new FileStream(strPath, FileMode.Open);
                    Image image = Image.FromStream(files);
                    pictureBox.Image = image;
                    files.Close();
                    files.Dispose();
                    //image.Dispose();
                }
                catch
                {
                    Messages.ShowAlertDialog("图片损坏无法打开！", false);
                }
            });
        }

        /// <summary>
        /// 开始采集
        /// </summary>
        private void btnStartCollect_Click(object sender, EventArgs e)
        {
            try
            {
                btnUpLoadData.Enabled = false;
                mf.timer2.Enabled = false;
                Thread.Sleep(500);
                btnStartCollect.Enabled = false;
                mf.indexPackages = 0;
                mf.isCollectImage = true;
                mf.totalPackages = 0;
                mf.deviceIndex = dIndex;
                MainForm.isSuccess = false;
                mf.recBuffer.Clear();
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x0C, new byte[2] { Convert.ToByte(dIndex), 0x01 }));
                
                Thread sub = new Thread(() =>
                {
                    if (!mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("开始采集图片超时！", false);
                    }
                });
                sub.IsBackground = true;
                sub.Start();
                //mf.timer2.Enabled = true;
                mf.imageStartTime = DateTime.Now;
                mf.RealDataType = 3;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("开始采集操作失败！", ex);
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        private void CameraPicture_FormClosing(object sender, FormClosingEventArgs e)
        {
            mf.RealDataType = 0;
            mf.imageDataPackages.Clear();
            mf.timer2.Enabled = true;
            mf.cameraPicture.Dispose();
            mf.cameraPicture = null;
        }

        /// <summary>
        /// 上报图片
        /// </summary>
        private void btnUpLoadData_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                Thread.Sleep(500);
                btnUpLoadData.Enabled = false;
                mf.deviceIndex = dIndex;
                mf.isCollectImage = false;
                MainForm.isSuccess = false;
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x0C, new byte[2] { Convert.ToByte(dIndex), 0x03 }));
                Thread sub = new Thread(() =>
                {
                    if (!mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("上报图片超时！", false);
                    }
                });
                sub.IsBackground = true;
                sub.Start();
                //mf.timer2.Enabled = true;
                mf.imageStartTime = DateTime.Now;
                mf.RealDataType = 5;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("上报图片操作失败！", ex);
            }
        }
    }
}
